<template>
  <div class="container">
    你好：{{userInfo.username}}|<el-button @click="dlgChangePwd=true" type="text">修改密码</el-button>
  </div>

  <el-dialog title="修改密码" v-model="dlgChangePwd" width="400px">
    <el-form label-width="80px" :model="formPwd" ref="formChangePwd" :rules="rules">
      <el-form-item label="原密码" prop="orgPwd">
        <el-input type="password" v-model="formPwd.orgPwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input type="password" v-model="formPwd.newPwd"></el-input>
      </el-form-item>
      <el-form-item label="重复输入" prop="reNewPwd">
        <el-input type="password" v-model="formPwd.reNewPwd"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
    <span class="dialog-footer">
      <el-button @click="dlgChangePwd = false">取 消</el-button>
      <el-button type="primary" @click="savePwd">确 定</el-button>
    </span>
    </template>
  </el-dialog>
</template>

<script>
import request from '@/util/request.js'
import {ElMessage, ElMessageBox} from "element-plus";
import {changePassword} from "../../api/user";
export default {
    name: 'UserCenter',
    data() {
      // 两次密码校验
      let checkPwd2=((rule,value,callback)=>{
        value = value.trim()
        if(value.length < 3 || value.length > 12){
          callback(new Error("密码长度在3-12个字符之间"));
        }else if(value != this.formPwd.newPwd){
          callback(new Error("两次次密码不一致"));
        }else{
          callback();
        }
      });
      return {
        userInfo: {
          id: '',
          username: '',
          avatar: ''
        },
        formPwd: {
          orgPwd: '',
          newPwd: '',
          reNewPwd: ''
        },
        dlgChangePwd: false,
        rules:{
          orgPwd:[ {required:true,message:"原始密码不能为空",trigger:'blur'} ],
          newPwd:[ {required:true,message:"新密码不能为空",trigger:'blur'} ],
          reNewPwd:[ {validator:checkPwd2, trigger:"blur"} ]
        }
      }
    },
    created() {
      this.getUserInfo()
    },
    methods: {
      getUserInfo() {
        request.get('/user/info').then(res=>{
          this.userInfo = res.data
        })
      },
      // 保存修改
      savePwd() {
        this.$refs['formChangePwd'].validate((valid) => {
          if(valid) {
            ElMessageBox.confirm('你正在修改密码, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(() => {
              let {orgPwd, newPwd} = this.formPwd
              changePassword(orgPwd, newPwd).then(res => {
                ElMessage.info(res.msg)
                this.logout()
              })
            }).catch(() => {
              ElMessage.info('取消修改密码')
            })
          }
        })
      },
      // 退出
      logout() {
        request.post('/user/logout').then(res=>{
          // console.log(res)
          this.$store.commit('resetState')
          this.$router.push('/login')
        })
      }
    }
}
</script>

<style scoped>
  .container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>